package com.xquant.pricing.systerm.service.task;

import com.xquant.pricing.systerm.service.JobService;
import com.xquant.pricing.utils.DateUtils;
import com.xquant.common.engine.xPP.cxf.XQuantPPService;
import org.apache.commons.collections.MapUtils;
import org.apache.commons.lang.StringUtils;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
/**
 * newCloud  定时任务
 *
 * */
@Component
public class JobServiceImpl4PP implements JobService {

    private static final Logger LOGGER = LoggerFactory.getLogger(JobServiceImpl4PP.class);
    @Autowired
    private XQuantPPService ppService;

    @Override
    public String getJobType() {
        return "PP_TASK";
    }

    @Override
    public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException {
        String begDate = MapUtils.getString(jobExecutionContext.getMergedJobDataMap(), "begDate");
        String endDate = MapUtils.getString(jobExecutionContext.getMergedJobDataMap(), "endDate");
        Integer offsetDays = MapUtils.getInteger(jobExecutionContext.getMergedJobDataMap(), "offsetDays");
        String context = MapUtils.getString(jobExecutionContext.getMergedJobDataMap(), "context");
        String jobName = MapUtils.getString(jobExecutionContext.getMergedJobDataMap(), "jobName");
        if (StringUtils.isEmpty(begDate)) {
            begDate = DateUtils.addDate(DateUtils.getCurrentDate(), offsetDays == null ? 0 : offsetDays);
            endDate = begDate;
        }
        for (String executeDate = begDate;
             executeDate.compareTo(endDate) <= 0;
             executeDate = DateUtils.addDate(executeDate, 1)) {

            TaskEntity taskEntity = new TaskEntity();
            taskEntity.setBaseDate(executeDate);
            taskEntity.setContext(context.replace("{baseDate}", executeDate));
            taskEntity.setJobName(jobName);
            JobScheduledPools.addTask(taskEntity);
        }
    }
}
